var checkProjecrPermission = function (userId, projectId) {
    return new Promise((resolve, rejected) => {
        query(`SELECT * FROM  project_user WHERE user_id = ? AND project_id = ?`, [userId, projectId], function (qerr, vals, fields) {
            if (qerr) {
                rejected(qerr);
            } else {
                if (vals.length == 0) {
                    rejected({
                        status: '403',
                        message: '权限不足'
                    });
                } else if (vals[0].permission == 'editor' || vals[0].permission == 'admin') {
                    resolve(vals[0].permission);
                } else {
                    rejected({
                        status: '403',
                        message: '权限错误'
                    });
                };
            };
        });
    });
};

var userDetail = function (userId) {
    return new Promise((resolve, rejected) => {
        query(`SELECT * FROM  user WHERE user_id = ?`, [userId], function (qerr, vals, fields) {
            if (qerr) {
                rejected(qerr);
            } else {
                if (vals.length == 0) {
                    rejected({
                        msg: '未找到用户'
                    });
                } else if (vals[0]) {
                    resolve(vals[0]);
                } else {
                    rejected({
                        msg: '未知错误'
                    });
                };
            };
        });
    });
};

module.exports = {
    CPP: checkProjecrPermission,
    CMP: '',
    userDetail: userDetail,
};